//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.vere.orm.db;

import com.vere.orm.db.pool.ConnectionPool;
import com.vere.orm.log.CLog;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

public class DBHelper {
    protected Connection conn;
    protected ResultSet rs = null;
    protected static ConnectionPool dataSource = null;

    public DBHelper() {
        if (dataSource == null) {
            dataSource = Configuration.getDataSource();
        }

    }

    public Connection openConnection() {
        try {
            this.conn = dataSource.getConnection();
        } catch (SQLException var2) {
            var2.printStackTrace();
        }

        return this.conn;
    }

    public Connection openTransactionConnection() {
        try {
            this.conn = dataSource.getConnection();
            this.conn.setAutoCommit(false);
        } catch (SQLException var2) {
            var2.printStackTrace();
        }

        return this.conn;
    }

    public void commit() {
        try {
            if (this.conn != null) {
                this.conn.commit();
            }
        } catch (SQLException var2) {
            var2.printStackTrace();
        }

    }

    public void rollback() {
        try {
            if (this.conn != null) {
                this.conn.rollback();
            }
        } catch (SQLException var2) {
            var2.printStackTrace();
        }

    }

    public boolean executeUpdate(String sql) {
        this.openConnection();

        try {
            Statement stmt = this.conn.createStatement();
            if (stmt.executeUpdate(sql) <= 0) {
                return false;
            }
        } catch (SQLException var6) {
            var6.printStackTrace();
            return false;
        } finally {
            CLog.debug("debug", sql);
            this.DBClose();
        }

        return true;
    }

    public ResultSet executeQuery(String sql) {
        this.openConnection();
        Statement stmt = null;

        try {
            stmt = this.conn.createStatement();
            this.rs = stmt.executeQuery(sql);
        } catch (SQLException var4) {
            var4.printStackTrace();
        }

        CLog.debug("debug", sql);
        return this.rs;
    }

    public boolean executeUpdate(String sql, Object[] params) {
        this.openConnection();

        try {
            PreparedStatement pstmt = this.conn.prepareStatement(sql);

            for(int i = 0; params != null && i < params.length; ++i) {
                if (params[i] instanceof Integer) {
                    pstmt.setInt(i + 1, Integer.parseInt(params[i].toString()));
                } else if (params[i] instanceof Long) {
                    pstmt.setLong(i + 1, Long.parseLong(params[i].toString()));
                } else if (params[i] instanceof Float) {
                    pstmt.setFloat(i + 1, Float.parseFloat(params[i].toString()));
                } else if (params[i] instanceof Double) {
                    pstmt.setDouble(i + 1, Double.parseDouble(params[i].toString()));
                } else if (params[i] instanceof Date) {
                    Date javaDate = (Date)params[i];
                    java.sql.Date sqlDate = new java.sql.Date(javaDate.getTime());
                    pstmt.setDate(i + 1, sqlDate);
                } else if (params[i] instanceof Calendar) {
                    Calendar javaDate = (Calendar)params[i];
                    Timestamp timestamp = new Timestamp(javaDate.getTimeInMillis());
                    pstmt.setTimestamp(i + 1, timestamp);
                } else if (params[i] instanceof String) {
                    pstmt.setString(i + 1, params[i].toString());
                }
            }

            if (pstmt.executeUpdate() <= 0) {
                return false;
            }
        } catch (SQLException var10) {
            var10.printStackTrace();
            return false;
        } finally {
            CLog.debug("debug", sql, params);
            this.DBClose();
        }

        return true;
    }

    public ResultSet executeQuery(String sql, Object[] params) {
        this.openConnection();

        try {
            PreparedStatement pstmt = this.conn.prepareStatement(sql);

            for(int i = 0; params != null && i < params.length; ++i) {
                if (params[i] instanceof Integer) {
                    pstmt.setInt(i + 1, Integer.parseInt(params[i].toString()));
                } else if (params[i] instanceof Long) {
                    pstmt.setLong(i + 1, Long.parseLong(params[i].toString()));
                } else if (params[i] instanceof Float) {
                    pstmt.setFloat(i + 1, Float.parseFloat(params[i].toString()));
                } else if (params[i] instanceof Double) {
                    pstmt.setDouble(i + 1, Double.parseDouble(params[i].toString()));
                } else if (params[i] instanceof Date) {
                    Date javaDate = (Date)params[i];
                    java.sql.Date sqlDate = new java.sql.Date(javaDate.getTime());
                    pstmt.setDate(i + 1, sqlDate);
                } else if (params[i] instanceof Calendar) {
                    Calendar javaDate = (Calendar)params[i];
                    Timestamp timestamp = new Timestamp(javaDate.getTimeInMillis());
                    pstmt.setTimestamp(i + 1, timestamp);
                } else if (params[i] instanceof String) {
                    pstmt.setString(i + 1, params[i].toString());
                }
            }

            this.rs = pstmt.executeQuery();
        } catch (SQLException var7) {
            var7.printStackTrace();
        }

        CLog.debug("debug", sql, params);
        return this.rs;
    }

    public ResultSet executeTransactionQuery(String sql) {
        Statement stmt = null;

        try {
            stmt = this.conn.createStatement();
            this.rs = stmt.executeQuery(sql);
        } catch (SQLException var4) {
            var4.printStackTrace();
        }

        CLog.debug("debug", sql);
        return this.rs;
    }

    public ResultSet executeTransactionQuery(String sql, Object[] params) {
        try {
            PreparedStatement pstmt = this.conn.prepareStatement(sql);

            for(int i = 0; params != null && i < params.length; ++i) {
                if (params[i] instanceof Integer) {
                    pstmt.setInt(i + 1, Integer.parseInt(params[i].toString()));
                } else if (params[i] instanceof Long) {
                    pstmt.setLong(i + 1, Long.parseLong(params[i].toString()));
                } else if (params[i] instanceof Float) {
                    pstmt.setFloat(i + 1, Float.parseFloat(params[i].toString()));
                } else if (params[i] instanceof Double) {
                    pstmt.setDouble(i + 1, Double.parseDouble(params[i].toString()));
                } else if (params[i] instanceof Date) {
                    Date javaDate = (Date)params[i];
                    java.sql.Date sqlDate = new java.sql.Date(javaDate.getTime());
                    pstmt.setDate(i + 1, sqlDate);
                } else if (params[i] instanceof Calendar) {
                    Calendar javaDate = (Calendar)params[i];
                    Timestamp timestamp = new Timestamp(javaDate.getTimeInMillis());
                    pstmt.setTimestamp(i + 1, timestamp);
                } else if (params[i] instanceof String) {
                    pstmt.setString(i + 1, params[i].toString());
                }
            }

            this.rs = pstmt.executeQuery();
        } catch (SQLException var7) {
            var7.printStackTrace();
        }

        CLog.debug("debug", sql, params);
        return this.rs;
    }

    public boolean executeTransactionUpdate(String sql) {
        try {
            Statement stmt = this.conn.createStatement();
            if (stmt.executeUpdate(sql) > 0) {
                return true;
            }
        } catch (SQLException var3) {
            var3.printStackTrace();
        }

        CLog.debug("debug", sql);
        return false;
    }

    public boolean executeTransactionUpdate(String sql, Object[] params) {
        try {
            PreparedStatement pstmt = this.conn.prepareStatement(sql);

            for(int i = 0; params != null && i < params.length; ++i) {
                if (params[i] instanceof Integer) {
                    pstmt.setInt(i + 1, Integer.parseInt(params[i].toString()));
                } else if (params[i] instanceof Long) {
                    pstmt.setLong(i + 1, Long.parseLong(params[i].toString()));
                } else if (params[i] instanceof Float) {
                    pstmt.setFloat(i + 1, Float.parseFloat(params[i].toString()));
                } else if (params[i] instanceof Double) {
                    pstmt.setDouble(i + 1, Double.parseDouble(params[i].toString()));
                } else if (params[i] instanceof Date) {
                    Date javaDate = (Date)params[i];
                    java.sql.Date sqlDate = new java.sql.Date(javaDate.getTime());
                    pstmt.setDate(i + 1, sqlDate);
                } else if (params[i] instanceof Calendar) {
                    Calendar javaDate = (Calendar)params[i];
                    Timestamp timestamp = new Timestamp(javaDate.getTimeInMillis());
                    pstmt.setTimestamp(i + 1, timestamp);
                } else if (params[i] instanceof String) {
                    pstmt.setString(i + 1, params[i].toString());
                }
            }

            if (pstmt.executeUpdate() > 0) {
                return true;
            }
        } catch (SQLException var7) {
            var7.printStackTrace();
        }

        CLog.debug("debug", sql, params);
        return false;
    }

    public void DBClose() {
        try {
            dataSource.close(this.conn, this.rs);
        } catch (SQLException var2) {
            var2.printStackTrace();
        }

    }
}
